﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using QiHe.CodeLib;
using ExcelLibrary.CompoundDocumentFormat;
using ExcelLibrary.BinaryDrawingFormat;
using ExcelLibrary.BinaryFileFormat;
using ExcelLibrary.SpreadSheet;


namespace UI_Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //btnShowFiles_Click(null, null);
            this.treeView1.AllowDrop = true;
            this.treeView1.CheckBoxes = true;           
            this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
            this.listBoxFile.SelectedIndexChanged += new EventHandler(this.lstFiles_SelectedIndexChanged);
            
        }

       Parfile a = new Parfile();
       
        
        private void btnShowFiles_Click(object sender, System.EventArgs e)
        {
            try
            {
                treeView1.Nodes.Clear();
                listBoxFile.Items.Clear();
                string[] drives = Environment.GetLogicalDrives();
                foreach (string drv in drives)
                {
                    TreeNode node = new TreeNode();
                    node.Text = drv;
                    treeView1.Nodes.Add(node);
                    FillDirectory(drv, node, 0);
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        private void FillDirectory(string drv, TreeNode parent, int level)
        {
            try
            {
                // I want to go only upto 3 level.
                level++;
                if (level > 4)
                    return;
                DirectoryInfo dir = new DirectoryInfo(drv);
                if (!dir.Exists)
                    throw new DirectoryNotFoundException
                        ("directory does not exist:" + drv);

                foreach (DirectoryInfo di in dir.GetDirectories())
                {
                    TreeNode child = new TreeNode();
                    child.Text = di.Name;
                    parent.Nodes.Add(child);

                    FillDirectory(child.FullPath, child, level);
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }

        }

        private void lstFiles_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            try
            {
                label1.Text = treeView1.SelectedNode.FullPath;
                string path = Path.Combine(treeView1.SelectedNode.FullPath, listBoxFile.SelectedItem.ToString());                
                string fullpath = path.Replace(@"\\",@"\");
                // Doc Excel 
                a.getopen(fullpath);
                a.loadexcel(tabControl1);
            }

            catch { }
        }

        private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            try
            {
                TreeNode node = e.Node;
                string strFullPath = node.FullPath;

                listBox1.DisplayFiles(strFullPath);
                label1.Text = treeView1.SelectedNode.FullPath;
                DirectoryInfo fi = new DirectoryInfo(strFullPath);              
            }
            catch { }

        }

        private void DisplayFiles(string dirName)
        {
            try
            {
               listBoxFile.Items.Clear();
                // test to see if directory exists
                DirectoryInfo dir = new DirectoryInfo(dirName);
                if (!dir.Exists)
                    throw new DirectoryNotFoundException
                        ("directory does not exist:" + dirName);

                // fill the directory listbox
                foreach (DirectoryInfo di in dir.GetDirectories())
                {
                    string str = "[Dir] " + di.Name;
                    listBoxFile.Items.Add(str);
                }

                // fill the Files listbox
                foreach (FileInfo fi in dir.GetFiles())
                {
                   listBoxFile.Items.Add(fi.Name);
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

                 
        
    }
    
}
